package com.hexb.smh.controller.management;

import com.hexb.core.common.Page;
import com.hexb.smh.entity.param.log.LoginLogQueryParam;
import com.hexb.smh.entity.param.log.OperationLogQueryParam;
import com.hexb.smh.entity.view.LoginLogView;
import com.hexb.smh.entity.view.OperationLogView;
import com.hexb.smh.service.IOperationLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

import static com.hexb.smh.utils.OperationConst.*;
import static com.hexb.smh.utils.SmhConstant.ROLE_ADMIN;
import static com.hexb.smh.utils.SmhConstant.ROLE_SUPER_ADMIN;

/**
 * @author : hexb
 */
@RestController
@RequestMapping("/management/logs")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "A9.日志", description = "Server", tags = {"A9.日志"})
public class LogController {

    @NonNull
    private final IOperationLogService operationLogService;

    @ApiOperation("操作日志")
    @GetMapping("/operation/{pageSize}/{pageNum}")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN})
    public Page<OperationLogView> firstLetter(OperationLogQueryParam param,
                                              @PathVariable int pageSize,
                                              @PathVariable int pageNum) {
        return operationLogService.search(param, pageSize, pageNum);
    }


    @ApiOperation("登录日志")
    @GetMapping("/login/{pageSize}/{pageNum}")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN})
    public Page<LoginLogView> firstLetter(LoginLogQueryParam param,
                                          @PathVariable int pageSize,
                                          @PathVariable int pageNum) {
        return operationLogService.search(param, pageSize, pageNum);
    }

    @ApiOperation("操作日志类型列表")
    @GetMapping("/operations")
    @RequiresRoles(logical = Logical.OR, value = {ROLE_SUPER_ADMIN, ROLE_ADMIN})
    public List<String> operations() {
        return Arrays.asList(
                APPEND_ADMIN,
                UPDATE_ADMIN,
                DELETE_ADMIN,
                ENABLE_ADMIN,
                DISABLE_ADMIN,
                RESET_ADMIN,
                CHANGE_PASSWORD,
                APPEND_SALESMAN,
                UPDATE_SALESMAN,
                DELETE_SALESMAN,
                ENABLE_SALESMAN,
                DISABLE_SALESMAN,
                RESET_SALESMAN,
                APPEND_PRODUCT,
                UPDATE_PRODUCT,
                DELETE_PRODUCT,
                TOGGLE_PRODUCT,
                IMPORT_PRODUCT,
                EDIT_CONFIG,
                APPEND_EVALUATE,
                UPDATE_EVALUATE,
                DELETE_EVALUATE
        );
    }


}
